import { _decorator, Button, Component, instantiate, Layout, Node, ScrollView, tween, v3 } from 'cc';
import { Daily_Mission, EVENT_TYPE, Long_Mission } from '../../Cube/Levels';
import { dailyMissionItem } from './dailyMissionItem';
import EventMgr from '../Tool/EventMgr';
import { longMissionItem } from './longMissionItem';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
import DatasSpace from '../Tool/DatasSpace';
import SkdComm from '../SkdComm';
import { AdComponent } from '../AdComponent';
const { ccclass, property } = _decorator;

@ccclass('PropMission')
export class PropMission extends Component {

  @property(Button) Btn_daily: Button = null;
  @property(Button) Btn_long: Button = null;

  @property(ScrollView) dailyMissionView: ScrollView = null;
  @property(ScrollView) longMissionView: ScrollView = null;

  private isInitLong: boolean = false;
  start() {
    this.switchMissionList(0);
    this.initDailyMissionView();
    this.onEvent();

  }


  private onEvent() {
    EventMgr.on(EVENT_TYPE.EVENT_UPD_DAILYMISSIONLIST, this.sortDailyMissionList, this);
  }

  private offEvent() {
    EventMgr.off(EVENT_TYPE.EVENT_UPD_DAILYMISSIONLIST, this.sortDailyMissionList, this);
  }

  protected onDestroy(): void {
    this.offEvent();
  }
  update(deltaTime: number) {

  }

  protected onDisable(): void {
    SkdComm.Instance.createGameClub();
  }
  protected onEnable(): void {
    AdComponent.Instance.showInterstitialFullShow();
    SkdComm.Instance.hideGameClub();

    this.scheduleOnce(() => {
      this.sortDailyMissionList();
    })
  }

  btnClose() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    this.node.active = false;
  }

  initDailyMissionView() {
    console.log('每日任务', DatasSpace.Instance.isSameDay)

    let content = this.dailyMissionView.content;
    let oriItem = content.children[0];
    for (let i = 0; i < Daily_Mission.length; i++) {
      let newItem = content.children[i];
      if (!newItem) {
        newItem = instantiate(oriItem)
        newItem.setParent(oriItem.parent)
      }
      newItem.name = `item_${i}`;
      newItem.getComponent(dailyMissionItem).initMission(i, Daily_Mission[i]);
    }
  }

  private sortDailyMissionList() {
    let content = this.dailyMissionView.content;
    let isCompItem: Node[] = [];
    let isGetItem = [];
    let otherItem: Node[] = [];
    content.children.forEach((item, index) => {
      let _item = item.getComponent(dailyMissionItem);
      //console.log(_item.itemType.id, _item.itemType.isComplete, _item.itemType.isGet)
      if (_item.itemType.isComplete && !_item.itemType.isGet) {
        isCompItem.push(item);
      } else if (_item.itemType.isGet) {
        isGetItem.push(item);
      } else {
        otherItem.push(item);
      }
    })

    isGetItem.sort((a, b) => {
      let a_item = a.getComponent(dailyMissionItem);
      let b_item = b.getComponent(dailyMissionItem);
      return a_item.itemType.id - b_item.itemType.id;
    });

    let newList = isCompItem.concat(otherItem).concat(isGetItem)
    newList.forEach((item: Node, index) => {
      item.setSiblingIndex(index)
    })

    content.active = false;
    content.active = true;

  }

  initLongMissionView() {
    let content = this.longMissionView.content;
    for (let i = 0; i < Long_Mission.length; i++) {
      let newItem = content.children[i];
      if (!newItem) {
        newItem = instantiate(content.children[0]);
        newItem.parent = content;
      }
      newItem.name = `item_${i}`;
      newItem.getComponent(longMissionItem).initMission(i);
    }
  }

  btnSwitch(event: TouchEvent, customData: string) {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    let type = parseInt(customData);
    this.switchMissionList(type);
  }

  switchMissionList(type) {
    if (!type) {
      this.Btn_daily.interactable = false
      this.Btn_long.interactable = true
      this.dailyMissionView.node.active = true;
      this.longMissionView.node.active = false;
      console.log('每日任务');
    } else {
      this.Btn_daily.interactable = true
      this.Btn_long.interactable = false
      this.dailyMissionView.node.active = false;

      if (!this.isInitLong) {
        this.initLongMissionView();
        this.isInitLong = true;
      }

      this.longMissionView.node.active = true;
      console.log('长期任务');
    }
  }
}


